import 'package:flutter_module/map/models/library_models.dart';
import 'package:flutter_module/map/ni_map_controller.dart';
import 'package:flutter_module/system/log_util.dart';

///处理地图相关的操作，记录地图相关的数据
class MapManager {
  late final NIMapController _mapController;

  // 工厂模式
  factory MapManager() => _getInstance();

  static final MapManager _instance = MapManager._internal();

  static MapManager get instance => _getInstance();

  NIMapController get mapController => _mapController;

  ///通过屏幕中心准星标记的marker(唯一的点，处理单点业务)
  NIMarker? _marker;

  NIMarker? get niMarker => _marker;


  List<NIGeoPoint> get points => _points;

  set points(List<NIGeoPoint> value) {
    _points = value;
  }

  ///用来记录线或面的点信息（处理单线或单面的业务）
  List<NIGeoPoint> _points = [];

  MapManager._internal() {
    LogUtil.instance.printf("MapManager 每次都要创建吗？？？？");
  }

  static MapManager _getInstance() {
    return _instance;
  }

  ///初始化获取地图控制器
  setNIMapController(NIMapController mapController) {
    LogUtil.instance.printf("MapManager 初始化Controller");
    _mapController = mapController;
  }

  ///使用准星的屏幕像素坐标增加marker
  Future addMarkerByForesight() async {
    //通过准星的key 获取准星所在屏幕位置

    //如果当前还没有添加marker则新添加一个，否则就改变当前marker的位置
    if (_marker == null) {
      _marker = await _mapController.addMarker();
    } else {
      _marker = await _mapController.addMarker(id: _marker!.id);
    }
  }

  Future addMarker(NIGeoPoint geoPoint) async {
    if (_marker == null) {
      _marker = await _mapController.addMarker(geoPoint: geoPoint);
    } else {
      _marker =
          await _mapController.addMarker(geoPoint: geoPoint, id: _marker!.id);
    }
  }

  ///移除当前添加的marker
  ///[_marker]
  Future removeMarker() async {
    if (_marker != null) {
      String id = _marker!.id;
      _marker = null;
      await _mapController.removeMarker(id);
    }
  }

  ///通过屏幕准星获取线的点
  ///维护[_points]
  Future addDrawLinePointByForesight() async {
    //如果当前还没有添加marker则新添加一个，否则就改变当前marker的位置
    _points = (await _mapController.addDrawLinePoint())!;
  }

  ///通过屏幕准星获取线的点
  ///维护[_points]
  Future addDrawPolygonPointByForesight() async {
    //如果当前还没有添加marker则新添加一个，否则就改变当前marker的位置
    _points = (await _mapController.addDrawPolygonPoint())!;
  }

// ///获取准星位置像素值
// Offset _getForesightCenter() {
//   //通过准星的key 获取准星所在屏幕位置
//   final RenderObject? renderBox =
//       ConstantConfig.mapForesightId.currentContext?.findRenderObject();
//   if (renderBox != null && renderBox is RenderBox) {
//     final positionGreen = renderBox.localToGlobal(Offset.zero);
//     //获取的屏幕位置要乘以屏幕密度系数
//     Offset offset = Offset(
//         (positionGreen.dx + ConstantConfig.   foresightSize / 2) *
//             ConstantConfig.devicePixelRatio,
//         (positionGreen.dy + ConstantConfig.foresightSize / 2) *
//             ConstantConfig.devicePixelRatio);
//     return offset;
//   } else {
//     return Offset(
//         ConstantConfig.screenSize.width / 2 * ConstantConfig.devicePixelRatio,
//         ConstantConfig.screenSize.height /
//             2 *
//             ConstantConfig.devicePixelRatio);
//   }
// }
}
